<?php
namespace App\Service\Admin\Qudao;

use App\Service\Admin\BaseService as Service;

class ShopService extends Service
{
    //渠道商

    protected $isauths = [
        '未审核', '已审核',
    ];

    /**
     * 列表
     * @param int $isauth
     * @param array $pages【start，limit】
     */
    public function getList($isauth = 0, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->db2Conn($this->dbQdShop)->where('del', 0);
        if ($isauth) {
            $db = $db->where('is_check', 0);
        }
        $total = $db->count();
        $res = $db->orderBy('id', 'desc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $isauths = $this->isauths;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $auth_name = array_key_exists($v['is_check'], $isauths) ? $isauths[$v['is_check']] : '-';
            if (substr($v['avatarUrl'], 0, 4) == 'http') {
                $avatarUrl = $v['avatarUrl'];
            } else {
                $avatarUrl = $v['avatarUrl'] ? env('DOMAIN') . '/'. $v['avatarUrl'] : '';
            }
            $arys[] = [
                'id' => $v['id'],
                'nickname' => $v['nickname'],
                'phone' => $v['phone'],
                'avatarUrl' => $avatarUrl,
                'serial' => $v['serial'],
                'level' => $v['level'],
                'parent_id' => $v['parent_id'],
                'isauth' => $v['is_check'],
                'auth_name' => $auth_name,
                'balance' => $v['balance'] / 100,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    /**
//     * 审核OK
//     * @param int $id 分类ID
//     */
//    public function setAuth($id)
//    {
//        $res = (array) $this->db2Conn($this->dbQdShop)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '记录不存在');
//        }
//        $this->db2Conn($this->dbQdShop)->where('id', $id)->update(['is_check' => 1]);
//        return $this->toback();
//    }

    /**
     * 审核OK
     * @param int $id
     * @param int $level
     */
    public function setAuth($id, $level)
    {
        $res = (array) $this->db2Conn($this->dbQdShop)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '记录不存在');
        }
        $arys = [
            'level' => $level,
            'is_check' => 1,
        ];
        $this->db2Conn($this->dbQdShop)->where('id', $id)->update($arys);
        return $this->toback();
    }

    //充值记录
    public function rechrgeList($uid)
    {
        $res = $this->db2Conn($this->dbQdPay)
            ->where('type', 1)
            ->where('status', 2)
            ->where('uid', $uid)
            ->skip(0)->take(10)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $shopOne = (array) $this->dbwbConn($this->dbQdShop)
                ->where('id', $v['uid'])
                ->first();
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => empty($shopOne) ? '-' : $shopOne['nickname'],
                'money' => toNumber($v['money']),
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //提现记录
    public function tixianList($uid)
    {
        $res = $this->db2Conn($this->dbQdTixian)
            ->where('status', 2)
            ->where('uid', $uid)
            ->skip(0)->take(10)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'money' => $v['amount'] / 100,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //消费记录
    public function costList($uid)
    {
        $res = $this->db2Conn($this->dbQdPay)
            ->where('type', 2) //订单消费
            ->where('status', 2) //支付成功
            ->where('uid', $uid)
            ->skip(0)->take(10)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'money' => $v['amount'] / 100,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //团队
    public function teamList($pid = 0)
    {
        $pOne = (array) $this->db2Conn($this->dbQdShop)->where('id', $pid)->first();
        $pname = empty($pOne) ? '-' : $pOne['nickname'];
        $db = $this->db2Conn($this->dbQdShop);
        if ($pid) {
            $db = $db->where('parent_id', $pid);
        }
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            if (substr($v['avatarUrl'], 0, 4) == 'http') {
                $avatarUrl = $v['avatarUrl'];
            } else if (file_exists($v['avatarUrl'])) {
                $avatarUrl = env('DOMAIN') . '/'. $v['avatarUrl'];
            } else {
                $avatarUrl = env('FC_DOMAIN') . '/'. $v['avatarUrl'];
            }
            $cost = $this->dbwbConn($this->dbQdIncome)
                ->where('uid', $v['id'])
                ->sum('amount');
            $arys[] = [
                'id' => $v['id'],
                'pname' => $pname,
                'nickname' => $v['nickname'],
                'phone' => $v['phone'],
                'avatarUrl' => $avatarUrl,
                'serial' => $v['serial'],
                'level' => $v['level'],
                'balance' => $v['balance'] / 100,
                'cost' => $cost / 100,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //团队收益明细
    public function teamCostList($pages, $uid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $res = $this->dbwbConn($this->dbQdIncome)
            ->where('uid', $uid)
            ->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'money' => toNumber($v['amount']),
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //提现审核列表
    public function txAuthList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbwbConn($this->dbQdTixian)
            ->where('del', 0)
            ->where('status', 1);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbwbConn($this->dbQdShop)
                ->where('del', 0)
                ->where('id', $v['uid'])
                ->first();
            $uname = !empty($uOne)&&$uOne['nickname'] ? $uOne['nickname'] : '-';
            $phone = !empty($uOne)&&$uOne['phone'] ? $uOne['phone'] : '-';
            $status_name = '审核中';
            if ($v['status'] > 1) {
                $status_name = $v['status']==2 ? '已提现' : '提现失败';
            }
            //账号
            $bank_info = '-';
            $bankOne = (array) $this->dbConn($this->dbUserWithdrawInfo)
                ->where('del', 0)
                ->where('uid', $v['uid'])
                ->where('is_default', 1)
                ->first();
            if (!empty($bankOne)) {
                $bank_info = $bankOne['bank_name'].'：'.$bankOne['card_no'];
            }
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'phone' => $phone,
                'amount' => toNumber($v['amount']),
                'bank_info' => $bank_info,
                'status' => $v['status'],
                'status_name' => $status_name,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //提现审核动作
    public function txAuthModify($id, $auth)
    {
        $res = (array) $this->dbwbConn($this->dbQdTixian)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['status'] == 2) {
            return $this->toback(-2, '已提现');
        }
        if ($res['status'] == 3) {
            return $this->toback(-2, '已拒绝');
        }
        $auth = $auth==1 ? 3 : 2;
        $up = $this->dbwbConn($this->dbQdTixian)->where('id', $id)
            ->update(['status' => $auth, 'approvetime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    //卡号列表
    public function bankList($pages, $uid)
    {
        $this->bankDefaultInit($uid);
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('del', 0)
            ->where('uid', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbwbConn($this->dbQdShop)
                ->where('del', 0)
                ->where('id', $v['uid'])
                ->first();
            $uname = !empty($uOne)&&$uOne['nickname'] ? $uOne['nickname'] : '-';
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'realname' => $v['name'],
                'phone' => $v['phone'],
                'bank_name' => $v['bank_name'],
                'card_no' => $v['card_no'],
                'is_default' => $v['is_default'],
                'default' => $v['is_default'] ? '是' : '否',
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //卡号默认
    public function bankDefaultModify($id)
    {
        $res = (array) $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //取消默认
        $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('uid', $res['uid'])
            ->update(['is_default' => 0]);
        //设置默认
        $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('id', $id)
            ->update(['is_default' => 1]);
        return $this->toback();
    }

    //卡号默认初始化
    public function bankDefaultInit($uid)
    {
        $total = $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('del', 0)
            ->where('uid', $uid)
            ->count();
        if ($total == 0) {
            return false;
        }
        if ($total > 1) {
            return true;
        }
        $this->dbwbConn($this->dbDistributorWithdrawInfo)
            ->where('del', 0)
            ->where('uid', $uid)
            ->update(['is_default' => 1]);
        return true;
    }
}